#!/usr/bin/python
#coding:utf-8

#author: Freev

import  threading
import linecache
import re
import sys
import  pymongo


def is_real(host,num):
    try:
        client = pymongo.MongoClient(host,27017,waitQueueTimeoutMS=50,waitQueueMultiple=50)    
        print client.database_names()
        if client.database_names():
            print host,'ok'
            gitfile.write(host+'\n'+str(client.database_names())+'\n')
        else: print host,'not really'
        client.close()
    except Exception,ex:
        print ex

def getlin(li):
    con=linecache.getline(hostpath,li).strip('\n\r\t')
    if con: return con
    else: return None

def main():
    x=0
    a=len(linecache.getlines(hostpath))
    threads=[]
    th=30
    while x<a:
        flag=0
        for i in range(x,th+x):
            if getlin(i+1):
                flag+=1
                temp=threading.Thread(target=is_real,args=(getlin(i+1),i))
                threads.append(temp)
            else: break
        for i in range(x,len(threads)):
            threads[i].start()
        for i in range(x,len(threads)):
            threads[i].join()
        x=x+th

if __name__ =='__main__':

    
    if len(sys.argv)==3:
        hostpath=sys.argv[1]
        gitpath=sys.argv[2]
       
        gitfile=open(gitpath,'a')
        main()
    else:  print "python mon.py   host.txt    mon.txt "
